White's Studio.

LintCode- 669. Coin ChangeFollow

2018/08/24 Share

LintCode- 669. Coin ChangeFollow

Description

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.

Have you met this question in a real interview? Yes

Problem Correction

Example

Given coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Given coins = [2], amount = 3
return -1.

Analyst

  1. 最后一步:

    选上最后一个硬币: F[n][m] = F[n-1][m - A[k]] + 1

    不选最后一个硬币: F[n][m]= F[n-1][m]

  2. 初始状态:

    F[k][0] = 0

  3. 状态方程:

    F[n][m] = Math.min(F[n-1][m-A[k]] + 1, F[n-1][m])

CATALOG
  1. 1. LintCode- 669. Coin ChangeFollow
    1. 1.0.1. Description
    2. 1.0.2. Example
    3. 1.0.3. Analyst